import {RandTerrain} from "../utils/rand.mjs";

let randTerrain = new RandTerrain()

export class Tile {
    #div
    #visited
    #classes

    constructor(container = null) {
        // this.#terrain = terrain
        // this.items = []
        // this.npc = null
        this.#visited = false
        this.#createDiv()
        this.#classes = this.#div.classList
        if (container)
            container.appendChild(this.#div)
        // this.setStyleClasses('cloaked')
    }

    addStyleClasses(...classes) {
        if (this.#visited) // 如果当前 tile 已经被访问过，
            classes = classes.filter(e => !['peek', 'cloaked'].includes(e))
        this.#classes.add(...classes)
        // if(this.#visited)
        //     ['peek', 'cloaked'].forEach(e => this.#classes.remove(e))
    }

    toggleStyleClasses(...classes) {
        classes.forEach(c => this.#classes.toggle(c))
    }

    removeStyleClasses(...classes) {
        this.#classes.remove(...classes)
        this.#visited = !this.#classes.contains('cloaked')
    }

    setStyleClasses(classes) {
        this.#div.classList = `cell ${classes}`
    }

    #createDiv() {
        this.#div = document.createElement('div')
        this.#div.classList = `cell ${randTerrain.go()} cloaked`
        // this.div.classList.add(randTerrain.go(), 'cloaked')
    }
}

// let a = new Tile()
// a.addStyleClasses('x', 'y')
// a.addStyleClasses('z', 'w')

/*export class Tile {
    #terrain
    #states = ['', 'visited', 'cloaked', 'peek', 'player']  // 'visited'
    constructor(terrain, npc = null) {
        this.#terrain = terrain
        // this.items = []
        // this.npc = npc
        this.div = this.#createDiv()
        this.setStyleClasses('cloaked')
    }

    getStyleClasses(state=''){
        console.assert(this.#states.includes(state))

        const isVisited = ['', 'visited', 'player'].includes(state)

        if (state === 'player')
            return [state]

        let styleClasses = ['cell', this.#terrain]
        if (isVisited)
            return styleClasses

        styleClasses.push(state)
        return styleClasses
    }

    setStyleClasses(state='', terrain=''){
        if (terrain)
            this.#terrain = terrain

        let styleClasses = this.getStyleClasses(state)

        this.div.classList = ''
        this.div.classList.add(...styleClasses)
        // styleClasses.forEach((style) => this.div.classList.add(style))
    }

    #createDiv() {
        return document.createElement('div')
    }
}*/
